package com.example.demo.service.impl;

import com.example.demo.admin.dto.SearchDto;
import com.example.demo.admin.pojo.Dept;
import com.example.demo.admin.pojo.Job;
import com.example.demo.admin.pojo.PageResult;
import com.example.demo.mapper.JobMapper;
import com.example.demo.mapper.UserMapper;
import com.example.demo.service.JobService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

@Service
@Slf4j
public class JobServiceImpl implements JobService {

    @Autowired
    private JobMapper jobMapper;
    @Autowired
    private UserMapper userMapper;

    /**
     * 获取全部职业信息
     *
     * @return
     */
    @Override
    public PageResult getAllJob(SearchDto searchDto) {
        PageHelper.startPage(searchDto.getCurrentPage(), searchDto.getPageSize());
        Page<Job> page = jobMapper.getAllJob(searchDto);
        return PageResult.builder()
                .total(page.getTotal())
                .records(page.getResult())
                .build();
    }

    /**
     * 删除职业
     */
    @Override
    @Transactional
    public void deleteJobById(Long id) {
        jobMapper.deleteJobById(id);
        //删除职业后要将原本的员工职业改为空
        userMapper.updateJob(id);
    }

    /**
     * 添加职业
     *
     * @param
     */
    @Override
    public void addJob(String name) {
        Job job = Job.builder()
                .updateTime(LocalDateTime.now())
                .createTime(LocalDateTime.now())
                .name(name)
                .build();
        jobMapper.addJob(job);
    }

    /**
     * 编辑职业
     */
    @Override
    public void editJob(String name, Long id) {
        Job job = Job.builder()
                .id(id)
                .name(name)
                .updateTime(LocalDateTime.now())
                .build();
        jobMapper.editJob(job);
    }
}
